package com.course.android.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.baselib.android.cach.PreferceKey;
import com.baselib.android.cach.PreferceManager;
import com.baselib.android.utils.ToastTool;
import com.course.android.R;
import com.course.android.activity.base.BaseNetActivity;
import com.course.controller.UserController;
import com.course.model.HttpConfigBean;
import com.course.model.resp.UserVo;

import org.apache.commons.lang.StringUtils;

import java.util.List;


public class LoginActivity extends BaseNetActivity {
    ProgressBar loading;
    EditText username,password;
    @Override
    protected void initView() {
        loading = findViewById(R.id.loading);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tel = username.getText().toString();
                String pw = password.getText().toString();
                if (StringUtils.isBlank(tel)||StringUtils.isBlank(pw)){
                    ToastTool.makeText(getApplicationContext(),"参数不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                UserController.getInstance().loginReg(tel,pw,LoginActivity.this);
            }
        });



    }

    @Override
    protected void initData() {
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void onActivityResultCallBack(Integer reqCode, Intent data) {

    }
    boolean login;
    @Override
    public <T> void onData(HttpConfigBean flag, T o) {
        UserVo userVo =  (UserVo)o;
        ToastTool.makeText(getApplicationContext(),"登陆成功", Toast.LENGTH_LONG).show();
        PreferceManager.getInsance().saveValueBYkey(LoginActivity.this, PreferceKey.USER_INFO, JSONObject.toJSONString(userVo));
        PreferceManager.getInsance().saveValueBYkey(LoginActivity.this, PreferceKey.TOKEN, userVo.getToken());
        login = true;
    }

    @Override
    public <T> void onPageData(HttpConfigBean flag, int pageNum, int pageSize, int total, List<T> array) {

    }

    @Override
    public void onReqStart(HttpConfigBean flag) {
//        loading.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReqFinish(HttpConfigBean flag) {
//        loading.setVisibility(View.GONE);
        if (login){
            finish();
        }
    }

}
